package org.test.jung.main;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Paint;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.collections15.Transformer;
import org.cocopapaya.anttsp.domain.AntTspSimulation;
import org.cocopapaya.anttsp.domain.City;
import org.cocopapaya.anttsp.domain.Path;
import org.cocopapaya.anttsp.domain.Quantity;
import org.cocopapaya.anttsp.domain.Tour;

import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

public class SimpleGraphView {

	static Graph<City, Path> g2;

	static VisualizationViewer<City, Path> vv;

	static ISOMLayout<City, Path> layout;

	static AntTspSimulation simulation;
	
	static JFormattedTextField stepField;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		simulation = new AntTspSimulation();
		g2 = simulation.getGraph();

		System.out.println("The graph g2 = " + g2.toString());

		// The Layout<V, E> is parameterized by the vertex and edge types
		layout = new ISOMLayout<City, Path>(g2);

		layout.setSize(new Dimension(1000, 1000)); // sets the initial size of
		// the
		// space
		// The BasicVisualizationServer<V,E> is parameterized by the edge types
		vv = new VisualizationViewer<City, Path>(layout);
		vv.setPreferredSize(new Dimension(1000, 1000)); // Sets the viewing area
		// size

		// Setup up a new vertex to paint transformer...
		Transformer<City, Paint> vertexPaint = new Transformer<City, Paint>() {
			public Paint transform(City i) {
				
				if( i.contains(simulation.getCameraAnt())){
					return Color.YELLOW;
				}else{
					return Color.GREEN;
				}
				
				
			}
		};

		Transformer<Path, Stroke> pathStroke = new Transformer<Path, Stroke>() {

			public Stroke transform(Path arg0) {
				Tour shortest = simulation.getShortestTour();
				
				Logger.getInstance().log(shortest.toString());

				if (shortest.contains(arg0)) {
				
					return new BasicStroke((float) 2.0);
				} else {
					return new BasicStroke();
				}

				
			}

		};

		Transformer<Path, Paint> pathPaint = new Transformer<Path, Paint>() {

			public Paint transform(Path arg0) {

				Color c;

				Quantity p = arg0.getPheromone();
				double v = p.doubleValue();
				float f = (float) (1.0 / (v + 1.0)); // for v ->infinity,
				// f->0, and for v->0,
				// f->1

				c = new Color((float) 1.0 - f, (float) f, (float) 0.0);

				return c;
			}

		};

		vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);

		vv.getRenderContext().setVertexLabelTransformer(
				new ToStringLabeller<City>());
		vv.getRenderContext().setEdgeLabelTransformer(
				new ToStringLabeller<Path>());
		vv.getRenderContext().setEdgeDrawPaintTransformer(pathPaint);
		vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
		vv.getRenderContext().setEdgeStrokeTransformer(pathStroke);
		
		
		// Create a graph mouse and add it to the visualization component
		DefaultModalGraphMouse<Integer, String> gm = new DefaultModalGraphMouse<Integer, String>();
		gm.setMode(DefaultModalGraphMouse.Mode.TRANSFORMING);
		vv.setGraphMouse(gm);

		JFrame frame = new JFrame("Simple Graph View");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//frame.setLayout(new GridLayout());\
		Container contentPane = frame.getContentPane();
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		contentPane.add(vv);
		JButton actionButton = new JButton("Run");

		stepField = new JFormattedTextField(NumberFormat.getNumberInstance());
		stepField.setValue(new Long(1));
		stepField.setColumns(15);
		
		
		actionButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				
				Long i = (Long) stepField.getValue();
				
				//simulation.runTours(i.intValue());
				simulation.runSteps(i.intValue());
				
				vv.repaint();

			}

		});

		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		panel.add(actionButton);
		panel.add(stepField);
		
		
		

		contentPane.add(panel);
		frame.pack();
		frame.setVisible(true);

	}

}
